<template>
	<view>
		退货

		<button type="default" @tap="uploadImg">图片上传</button>
		<image v-for="item in imgList" :src="item" mode=""></image>
		<button type="default" @tap="upLoad">确定</button>
	</view>
</template>

<script>
import baseUrl from "@/api/baseUrl.js"
export default {
	data() {
		return {
			imgList:[]
		};
	},
	methods:{
		upLoad(){
			var arr=[]
			this.imgList.forEach(ele =>{
				var pro = new Promise((resolve,rejected)=>{
					uni.uploadFile({
						url:baseUrl + "/lejuClient/orderReturn/uploadImg",
						filePath:ele,
						name:"file",
						success(res) {
							resolve(JSON.parse(res.data).data.fileUrl)
						}
					})
				})
				arr.push(pro)
			})
			console.log(arr,arr.length);

			// / 如果想一次性获取多个promise实例保存的数据
			Promise.all(arr)
			.then(res =>{
				console.log(res)
		},
		uploadImg(){
			var _this = this;
			uni.chooseImage({
				success(res){
					console.log(res);
					var list = res.tempFilePaths;
					if(list.length >3){
						uni.showToast({
							title:"图片数量超出三张",
							icon:"none"
						})
						return
					}
					// 1m = 1024kb 1kb  = 1204
					var selectedList = res.tempFiles.find(ele =>ele.size >1024*1024);
					if(selectedList){
						uni.showToast({
							title:"选取的图片尺寸大于1m,请重新上传",
							icon:"none"
						})
						return
					}
					if(_this.imgList.length + list.length >3){
						uni.showToast({
							title:"选取的图片总数大于三张,请重新上传",
							icon:"none"
						})
						return
					}
					_this.imgList.push(...list);
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped></style>
